DIR=$(shell pwd)
BIN_DIR=$(DIR)/bin
LIB_DIR=$(DIR)/lib
SRC_DIR=$(DIR)/src
INCLUDE_DIR=$(DIR)/src
OBJ_DIR=$(DIR)/obj
DEPS_DIR=$(DIR)/deps
LIBNAME=eventrpc
LIB_NAME=lib$(LIBNAME).a
LIB=$(LIB_DIR)/lib$(LIBNAME).a
SAMPLES_DIR=$(DIR)/samples
INSTALL_DIR=/usr/lib
INSTALL_INCLUDE_DIR=/usr/include/eventrpc
LIBS=-lprotobuf -levent

EXTENSION=cpp
OBJS=$(patsubst $(SRC_DIR)/%.$(EXTENSION), $(OBJ_DIR)/%.o,$(wildcard $(SRC_DIR)/*.$(EXTENSION)))
DEPS=$(patsubst $(OBJ_DIR)/%.o, $(DEPS_DIR)/%.d, $(OBJS))

INCLUDE=-I$(INCLUDE_DIR)

CC=g++
STRIP=strip
CFLAGS=-Wall -W -g -Werror
STRIP_FLAGS=-g

.PHONY: all clean install uninstall rebuild test

all:$(OBJS)
	ar rcs $(LIB) $(OBJS)

sinclude $(DEPS)

$(DEPS_DIR)/%.d: $(SRC_DIR)/%.$(EXTENSION)
	@$(CC) -MM $(INCLUDE) $< > temp; \
	sed 1's,^,$(OBJ_DIR)/,' < temp > $@; \
	rm -f temp

$(OBJ_DIR)/%.o:$(SRC_DIR)/%.$(EXTENSION)
	$(CC) $< -o $@ -c $(CFLAGS) $(INCLUDE)

client:$(SAMPLES_DIR)/client.cpp $(LIB)
	$(CC) $< -o $(BIN_DIR)/$@ -L$(LIB_DIR) -l$(LIBNAME) $(LIBS) $(CFLAGS) $(INCLUDE) samples/echo.pb.o

server:$(SAMPLES_DIR)/server.cpp $(LIB)
	$(CC) $< -o $(BIN_DIR)/$@ -L$(LIB_DIR) -l$(LIBNAME) $(LIBS) $(CFLAGS) $(INCLUDE) samples/echo.pb.o

install:

uninstall:
	rm -f $(INSTALL_DIR)/$(LIB_NAME)
	rm -fr ${INSTALL_INCLUDE_DIR}

rebuild: clean all

clean:
	rm -rf $(OBJ_DIR)/* $(LIB_DIR)/* $(BIN_DIR)/*
	rm -fr $(DEPS_DIR)/*

